<!--发布票据 -->
<template>
    <div class="content">
        <div class="detail_mian" v-for="(item,index) in this.batchBillList" :key="item.index">
            <el-row class="title_block hpx-title-border posted-info">
                <span class="title_text">票据信息</span>
                <span class="title-price"><i class="iconfont icon-jine iconfont_right"></i>票面金额：<i class="title-price-i">{{item.bill_sum_price == 0 || item.bill_sum_price == '' ? '--' : formatPrice(item.bill_sum_price)}}{{item.bill_sum_price == 0 || item.bill_sum_price == '' ? '' : '万'}}</i></span>
                <span class="title-day"><i class="iconfont icon-xuanzerili iconfont_right"></i>到期日：<i v-if="item.bill_deadline_time == '' || item.bill_deadline_time == null">--</i><i v-if="item.bill_deadline_time != '' && item.bill_deadline_time != null">{{item.bill_deadline_time | formatDate}}</i></span>
                <span class="title-flaw">瑕疵情况：<i v-if="item.bill_flaw_ids.indexOf(1500) == -1">有</i><i v-if="item.bill_flaw_ids.indexOf(1500) > -1">无</i></span>
                <span class="top_btn">
                    <el-button type="danger" size="small" class="post-delete" @click="deleteBill(index)" v-if="batchBillList.length > 1">
                        <i class="iconfont icon-shanchu"></i>删除
                    </el-button>
                    <el-button type="text" size="small" class="pack-up" @click="pickUpDetail(item)" v-if="billId == 0">
                        {{item.bill_detail ? '收起' : '展开'}} <i :class="[item.bill_detail ? 'el-icon-arrow-up' : 'el-icon-arrow-down']"></i>
                    </el-button>
                </span>
            </el-row>
            <div class="mian_block" v-if="item.bill_detail">
                <el-form class="form-box" label-width="80px" :model="item" ref="form" :rules="rules">
                    <el-row class="posted-form-con">
                        <el-col :span="24" class="posted_img_title posted_title_img">
                            <el-form-item label="电票所在户：" prop="account_id">
                                <el-select class="approve-int" v-model="item.account_id" placeholder="请选择">
                                    <el-option
                                            v-for="item in accountList"
                                            :key="item.account_id"
                                            :label="item.account_name"
                                            :value="item.account_id">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <div class="posted-rem" style="margin-top: 6px;">请选择正确的“电票所在户”，该户也将是提现到账户 <el-button type="text" @click="$router.push('/enterprise/judge')" v-if="accountList.length < 4">添加银行账户</el-button></div>
                            <div class="posted-rem posted-warm"><i class="iconfont icon-jingshi"></i>&nbsp;若错误选择户且此票已被签收，将导致线上交易取消交易！须先有卖方自行协调买方线下打款，兴业数金再凭买方打款凭证校验退款！</div>
                        </el-col>
                        <el-col :span="24" class="posted_title_img">
                            <el-form-item label="上传票据图片：" prop="bill_img"></el-form-item>
                            <div class="posted-img-box">
                                <div class="upload-img-box">
                                    <div class="img-box">
                                        <i class="iconfont icon-shangchuanshanchu delete-bill-btn" @click="removeFort(index)"></i>
                                        <ImageZoom
                                                :width='290'
                                                :height='210'
                                                :image="item.bill_front_photo_address"
                                        ></ImageZoom>
                                    </div>
                                    <div class="upload-btn" @click="listInx(index)">
                                        <UploaderImages @input="forntBillImg" uploadText="上传票据正面"></UploaderImages>
                                    </div>
                                </div>
                                <div class="upload-img-box">
                                    <div class="img-box">
                                        <i class="iconfont icon-shangchuanshanchu delete-bill-btn" @click="removeBack1(index)"></i>
                                        <ImageZoom
                                                :width='290'
                                                :height='210'
                                                :image="item.bill_back_photo_address1"
                                        ></ImageZoom>
                                    </div>
                                    <div class="upload-btn" @click="listInx(index)">
                                        <UploaderImages @input="backBillImg1" uploadText="上传背书面1"></UploaderImages>
                                    </div>
                                </div>
                                <div class="upload-img-box">
                                    <div class="img-box">
                                        <i class="iconfont icon-shangchuanshanchu delete-bill-btn" @click="removeBack2(index)"></i>
                                        <ImageZoom
                                                :width='290'
                                                :height='210'
                                                :image="item.bill_back_photo_address2"
                                        ></ImageZoom>
                                    </div>
                                    <div class="upload-btn" @click="listInx(index)">
                                        <UploaderImages @input="backBillImg2" uploadText="上传背书面2"></UploaderImages>
                                    </div>
                                </div>
                            </div>
                        </el-col>
                    </el-row>
                    <el-row class="post-int-con">
                        <el-col :span="12" class="posted-int-width">
                            <el-form-item label="票面金额" prop="bill_sum_price">
                                <el-input class="approve-int" v-model="item.bill_sum_price" placeholder="请输入" @input="inputPrice(index)" @change="changePrice(item.bill_sum_price,index)" onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')" suffix-icon="iconfont icon-yuan"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="到期日" prop="bill_deadline_time">
                                <el-date-picker
                                        v-model="item.bill_deadline_time"
                                        type="date"
                                        placeholder="选择日期"
                                        class="approve-int"
                                        :picker-options="pickerOptions"
                                        @change="changeDate(index)">
                                </el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24" class="bill-flaw">
                            <el-form-item label="瑕疵情况" prop="bill_flaw_ids">
                                <el-checkbox-group v-model="item.bill_flaw_ids" size="small" @change="changeCheckbox(item.bill_flaw_ids,index)">
                                    <el-checkbox-button :label="1500" class="none-flaw">无瑕疵</el-checkbox-button>
                                    <el-checkbox-button :label="1501" class="turn-flaw">回头
                                        <el-input controls-position="right" type="number" :disabled="item.bill_flaw_ids.indexOf(1501) == -1" onkeydown="javascript: return event.keyCode == 69 || event.keyCode == 107 || event.keyCode == 109 ? false : true" v-model="item.flaw_append" onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')"></el-input>次
                                        </el-checkbox-button>
                                    <el-checkbox-button :label="1506">重复</el-checkbox-button>
                                    <el-checkbox-button :label="1507">质押</el-checkbox-button>
                                    <el-checkbox-button :label="1512">保证</el-checkbox-button>
                                    <el-checkbox-button :label="1513">保理</el-checkbox-button>
                                    <el-checkbox-button :label="1514">上下不一致</el-checkbox-button>
                                    <el-checkbox-button :label="2202">其他</el-checkbox-button>
                                </el-checkbox-group>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="背书次数" prop="endorsement_number" class="flaw-num">
                                <el-input-number class="post-int-mun" v-model="item.endorsement_number" controls-position="right" :min="0"></el-input-number>
                            </el-form-item>
                        </el-col>
                        <div v-if="checks == 1">
                            <el-col :span="12" class="posted-int-width">
                                <el-form-item label="竞价方式" prop="bidding_mode">
                                    <el-radio-group v-model="item.bidding_mode" size="small">
                                        <el-radio-button label="3210">一口价</el-radio-button>
                                        <el-radio-button label="3212">自由竞价</el-radio-button>
                                    </el-radio-group>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="发布期限" prop="trade_appoint_day" class="flaw-num">
                                    <el-radio-group v-model="item.trade_appoint_day" size="small">
                                        <el-radio-button :label="0">T + 0</el-radio-button>
                                        <el-radio-button :label="1">T + 1</el-radio-button>
                                        <el-radio-button :label="2">T + N</el-radio-button>
                                    </el-radio-group>
                                    <el-input-number :min="2" :max="10" v-model="item.trade_appoint_day" v-if="item.trade_appoint_day > 1" controls-position="right"></el-input-number>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12" class="posted-int-width">
                                <el-form-item label="利率" prop="bill_rate">
                                    <el-input class="approve-int" v-model="item.bill_rate" @input="ratechange(index)" placeholder="请输入利率" onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')" suffix-icon="iconfont icon-icon-test4"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12" class="posted-int-width">
                                <el-form-item label="每十万加" prop="bill_every_plus">
                                    <el-input class="approve-int" v-model="item.bill_every_plus" @input="ratechange(index)" placeholder="请输入每十万加" onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')" suffix-icon="iconfont icon-yuan"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="6" class="posted-count-box posted-count posted_title_img">
                                <span>贴息(元)：</span><span>{{item.bill_deal_rate}}</span>
                            </el-col>
                            <el-col :span="6" class="posted-count posted_title_img">
                                <span>贴现额(元)：</span><span>{{item.bill_deal_price}}</span>
                            </el-col>
                        </div>
                    </el-row>
                </el-form>
            </div>
        </div>
        <div class="podted-show">
            <div>
                <span class="title-price hpx-label"><i class="iconfont icon-jine iconfont_right"></i>票面总金额(万元)：<i class="title-price-i">{{this.totalPrice == 0 || this.totalPrice == '' ? '--' : formatPrice(this.totalPrice)}}</i></span>
                <span class="title-day hpx-label"><i class="iconfont icon-xuanzerili iconfont_right"></i>票面总张数(张)：<i class="title-price-i">{{this.billFaceNum == 0 || this.billFaceNum == '' ? '--' : this.billFaceNum}}</i></span>
                <span class="top_btn bottom_btn" v-if="billId == 0">
                    <el-button size="small" class="add-bill" @click="addBill()"><i class="iconfont icon-tianjia"></i>继续添加票据</el-button>
                </span>
            </div>
        </div>
        <div>
            <el-row>
                <el-col :span="24">
                    <div class="submit-btn posted-submit-btn">
                        <el-button type="info" size="small" @click="go_back">取消</el-button>
                        <el-button type="primary" size="small" @click="saveBill('form')" v-if="billId == 0 && checks == 1">确认发布</el-button>
                        <el-button type="primary" size="small" @click="saveBill()" v-if="billId == 0 && checks == 2">确认录入</el-button>
                        <el-button type="primary" size="small" @click="editBill()" v-if="billId != 0 && checks == 1">重新发布</el-button>
                        <el-button type="primary" size="small" @click="editBill()" v-if="billId != 0 && checks == 2">重新录入</el-button>
                    </div>
                </el-col>
            </el-row>
        </div>
        <!-- 上传失败提示 -->
        <DialogInput ref="uploadFail" title="票据修改提示信息">
            <div class="rem-dialog-con">
                <el-row v-for="(item,index) in uploadFailMap.failResult" :key="item.failIndexs" class="uploadFailTips">
                    <span>第{{index+1}}张票需修改内容：</span>
                    <span class="uploadFailTips_inner">{{item.failContent}}</span>
                </el-row>
            </div>
            <span slot="Remfooter">
                <el-button type="primary" size="small" class="dialog-button" @click="closeDialog()">确定</el-button>
            </span>
        </DialogInput>
    </div>
</template>

<script lang="ts">
    import { Vue, Component, Watch } from 'vue-property-decorator';
    import WarmPrompt from '@component/common/WarmPrompt.vue';
    import UploaderImages from '@component/common/UploaderImages.vue';
    import ImageZoom from '@component/common/ImageZoom.vue';
    import moment from 'moment';
    import { mapState, mapActions } from 'vuex';
    import _ from 'lodash'
    import calc from 'calculatorjs'
    import DialogInput from '@component/common/DialogInput.vue' 
    @Component({
        components: {
            WarmPrompt,
            UploaderImages,
            ImageZoom,
            DialogInput
        },
        computed: {
            ...mapState([
                'userInfo'
            ])
        },
        methods: {
            ...mapActions([
                "getPolicy"
            ])
        }
    })
    export default class Posted extends Vue {
        isAddTemplate: boolean = false;
        form = {
            bill_front_photo_address: 'https://hpx-pc.oss-cn-beijing.aliyuncs.com/billImgTem/bill-front.png',
            bill_front_photo_id: '',
            bill_back_photo_address1: 'https://hpx-pc.oss-cn-beijing.aliyuncs.com/billImgTem/bill-brank.png',
            bill_back_photo_id1: '',
            bill_back_photo_address2: 'https://hpx-pc.oss-cn-beijing.aliyuncs.com/billImgTem/bill-brank.png',
            bill_back_photo_id2: '',
            bill_type_id: 101,
            bill_sum_price: '',
            bill_flaw_ids: [1500],
            bill_detail: true,
            bidding_mode: 3210,
            trade_appoint_day: 0,
            endorsement_number: 0,
            flaw_append: '',
            bill_number: '',
            acceptor_name: '',
            acceptor_cnaps: '',
            bill_deadline_time: '',
            bill_back_files: [],
            bill_rate: '',
            bill_every_plus: '',
            bill_deal_rate: '',
            bill_deal_price: '',
            every_plus: '',
            account_id: ''
        }
        rules = {
            account_id: [
                { required: true, message: '请选择电票所在户', trigger: 'change' }
            ],
            bill_sum_price: [
                {required: true, message: '请输入票面金额', trigger: 'blur'}
            ],
            bill_img: [
                {required: true, message: '请上传票面', trigger: 'blur'}
            ],
            bill_deadline_time: [
                {required: true, message: '请选择到期日', trigger: 'blur'}
            ],
            bill_flaw_ids: [
                {required: true, message: '请选择票据瑕疵', trigger: 'blur'}
            ],
            bidding_mode: [
                {required: true, message: '请选择竞价方式', trigger: 'blur'}
            ],
            trade_appoint_day: [
                {required: true, message: '请选择发布期限', trigger: 'blur'}
            ]
        }
        userInfo: any;
        accountList: Array<any> = [];
        listIten: string | number = '';
        totalPrice: string | number = '';
        billFaceNum: string | number = '';
        bill_back_files: Array<any> = [];
        checks: string | number = '';
        billId: string | number = '';
        rateModel: any = {};
        billModel: any = {};
        uploadFailMap: any = {};
        async getAccount () {
            let response = await this.$http.get(`paying/xingyeapi/account/getAccount/${this.userInfo.enterprise_id}`)
            if(response.status == 200){
                response.data.acct_list.forEach((i)=>{
                    if(i.status == 1){
                        this.accountList.push(i)
                    }
                })
            }
        }
        batchBillList = [{
            bill_front_photo_address: 'https://hpx-pc.oss-cn-beijing.aliyuncs.com/billImgTem/bill-front.png',
            bill_front_photo_id: '',
            bill_back_photo_address1: 'https://hpx-pc.oss-cn-beijing.aliyuncs.com/billImgTem/bill-brank.png',
            bill_back_photo_id1: '',
            bill_back_photo_address2: 'https://hpx-pc.oss-cn-beijing.aliyuncs.com/billImgTem/bill-brank.png',
            bill_back_photo_id2: '',
            bill_type_id: 101,
            bill_sum_price: '',
            bill_flaw_ids: [1500],
            bill_detail: true,
            bidding_mode: 3210,
            trade_appoint_day: 0,
            endorsement_number: 0,
            flaw_append: '',
            bill_number: '',
            acceptor_name: '',
            acceptor_cnaps: '',
            bill_deadline_time: '',
            bill_back_files: [],
            bill_rate: '',
            bill_every_plus: '',
            bill_deal_rate: '',
            bill_deal_price: '',
            every_plus: '',
            account_id: ''
        }]
        // 收起
        pickUpDetail (item) {
            item.bill_detail = !item.bill_detail;
        }
        // 点击改变瑕疵
        changeCheckbox (item,inx) {
            if(this.batchBillList[inx].bill_flaw_ids[this.batchBillList[inx].bill_flaw_ids.length-1] == 1500){
                this.batchBillList[inx].bill_flaw_ids = [1500]
            }else if(this.batchBillList[inx].bill_flaw_ids[this.batchBillList[inx].bill_flaw_ids.length-1] == 2202){
                this.batchBillList[inx].bill_flaw_ids = [2202]
            }
            else{
                if(this.batchBillList[inx].bill_flaw_ids.includes(1500)){
                    this.batchBillList[inx].bill_flaw_ids.splice(item.indexOf(1500),1)
                    this.batchBillList[inx].bill_flaw_ids = item;
                }else if(this.batchBillList[inx].bill_flaw_ids.includes(2202)){
                    this.batchBillList[inx].bill_flaw_ids.splice(item.indexOf(2202),1)
                    this.batchBillList[inx].bill_flaw_ids = item;
                }
            }
            if(this.batchBillList[inx].bill_flaw_ids.indexOf(1501) == -1){
                this.batchBillList[inx].flaw_append = '';
            }
        }
        listInx (inx: string | number) {
            this.listIten = inx;
        }
        formatPrice (price) {
            return calc.div(price, 10000)
        }
        // 上传正面
        forntBillImg (valData: any) {
            if(valData != null && valData != undefined){
                this.batchBillList[this.listIten].bill_front_photo_address = valData.path;
                this.batchBillList[this.listIten].bill_front_photo_id = valData.id;
                this.billFaceNum = this.batchBillList.length;
                this.billOcr(this.batchBillList[this.listIten].bill_front_photo_address);
                this.inputPrice(this.listIten)
            }
        }
        // 删除正面
        removeFort (index) {
            this.batchBillList[index].bill_front_photo_id = '';
            this.batchBillList[index].bill_front_photo_address = 'https://hpx-pc.oss-cn-beijing.aliyuncs.com/billImgTem/bill-front.png';
        }
        // 票据正面ocr
        billOcr (imageUrl: string) {
            let date = new Date();
            this.$http.get(`ocr/bill?adderss=${imageUrl}`)
                .then((response) => {
                    this.batchBillList[this.listIten].bill_number = response.data.bill_number;
                    this.batchBillList[this.listIten].acceptor_name = response.data.acceptor_name;
                    this.batchBillList[this.listIten].acceptor_cnaps = response.data.acceptor_cnaps;
                    this.batchBillList[this.listIten].bill_sum_price = response.data.bill_sum_price;
                    if(response.data.deadline_time == '' || response.data.deadline_time == null){
                        this.batchBillList[this.listIten].bill_deadline_time = moment(date).format('YYYY-MM-DD');
                    }else {
                        this.batchBillList[this.listIten].bill_deadline_time = moment(response.data.deadline_time).format('YYYY-MM-DD');
                    }
                    this.changePrice(this.batchBillList[this.listIten].bill_sum_price, this.listIten);
                })
        }
        // 上传背面1
        backBillImg1 (valData: any) {
            if(valData != null && valData != undefined){
                this.batchBillList[this.listIten].bill_back_photo_address1 = valData.path;
                this.batchBillList[this.listIten].bill_back_photo_id1 = valData.id;
                this.batchBillList[this.listIten].bill_back_files.push({ file_id: valData.id })
            }
        }
        // 上传背面2
        backBillImg2 (valData: any) {
            if(valData != null && valData != undefined){
                this.batchBillList[this.listIten].bill_back_photo_address2 = valData.path;
                this.batchBillList[this.listIten].bill_back_photo_id2 = valData.id;
                this.batchBillList[this.listIten].bill_back_files.push({ file_id: valData.id })
            }
        }
        // 删除背面
        removeBack1 (index) {
            this.batchBillList[index].bill_back_photo_id1 = '';
            this.batchBillList[index].bill_back_photo_address1 = 'https://hpx-pc.oss-cn-beijing.aliyuncs.com/billImgTem/bill-brank.png';
        }
        removeBack2 (index) {
            this.batchBillList[index].bill_back_photo_id2 = '';
            this.batchBillList[index].bill_back_photo_address2 = 'https://hpx-pc.oss-cn-beijing.aliyuncs.com/billImgTem/bill-brank.png';
        }
        // 输入票面金额
        changePrice (priceNum: string, index: string | number) {
            this.totalPrice = 0;
            if(this.batchBillList[index].bill_sum_price == undefined){
                this.batchBillList[index].bill_sum_price = '';
            }
            for (let i = 0; i< this.batchBillList.length; i ++) {
                this.totalPrice += Number(this.batchBillList[i].bill_sum_price);
            }
        }
        //清空利率，每十万加，贴息，贴现额，让用户重新填写利率和每十万加，防止直接传到后台导致贴息金额不对 。
        // 成功上传票面，修改到期日，修改票面金额时调用
        inputPrice (index:number) {
            this.batchBillList[index].bill_rate = ''
            this.batchBillList[index].bill_every_plus = ''
            this.batchBillList[index].bill_deal_rate = ''
            this.batchBillList[index].bill_deal_price = ''
        }
        //修改到期日
        changeDate (index) {
            this.inputPrice(index)
        }
        // 添加票据
        addBill () {
            this.form = {
                bill_front_photo_address: 'https://hpx-pc.oss-cn-beijing.aliyuncs.com/billImgTem/bill-front.png',
                bill_front_photo_id: '',
                bill_back_photo_address1: 'https://hpx-pc.oss-cn-beijing.aliyuncs.com/billImgTem/bill-brank.png',
                bill_back_photo_id1: '',
                bill_back_photo_address2: 'https://hpx-pc.oss-cn-beijing.aliyuncs.com/billImgTem/bill-brank.png',
                bill_back_photo_id2: '',
                bill_type_id: 101,
                bill_sum_price: '',
                bill_flaw_ids: [1500],
                bill_detail: true,
                bidding_mode: 3210,
                trade_appoint_day: 0,
                endorsement_number: 0,
                flaw_append: '',
                bill_number: '',
                acceptor_name: '',
                acceptor_cnaps: '',
                bill_deadline_time: '',
                bill_back_files: [],
                bill_rate: '',
                bill_every_plus: '',
                bill_deal_rate: '',
                bill_deal_price: '',
                every_plus: '',
                account_id: ''
            }
            let listBillTrade = _.cloneDeep(this.batchBillList);
            for (let i = 0; i < listBillTrade.length; i ++) {
                if (!listBillTrade[i].account_id){
                    this.$alert(`请选择票据所在户！`,'提示',{
                        confirmButtonText: '确定',
                        customClass: 'custom-message-box'
                    }).catch(() => {})
                    return;
                }
                if (!listBillTrade[i].bill_front_photo_id){
                    this.$alert('请上传票据正面图片！','提示',{
                        confirmButtonText: '确定',
                        customClass: 'custom-message-box'
                    })
                    return;
                }
                if (!listBillTrade[i].bill_sum_price){
                    this.$alert('请输入票面金额！','提示',{
                        confirmButtonText: '确定',
                        customClass: 'custom-message-box'
                    }).catch(() => {})
                    return;
                }
                if (listBillTrade[i].bill_sum_price <= 0){
                    this.$alert('请输入正确的票面金额！','提示',{
                        confirmButtonText: '确定',
                        customClass: 'custom-message-box'
                    }).catch(() => {})
                    return;
                }
                if (!listBillTrade[i].bill_deadline_time){
                    this.$alert('请选择票据到期日！','提示',{
                        confirmButtonText: '确定',
                        customClass: 'custom-message-box'
                    }).catch(() => {})
                    return;
                }
            }
            this.batchBillList.forEach((i) => {
                i.bill_detail = false;
            })
            this.batchBillList.push(this.form);
        }
        // 删除
        deleteBill (index) {
            this.billFaceNum = 0;
            this.totalPrice = 0;
            this.batchBillList.splice(index,1);
            for (let i = 0; i < this.batchBillList.length; i ++){
                if(this.batchBillList[i].bill_front_photo_id != ''){
                    this.billFaceNum += 1;
                }
            }
            for (let j = 0; j < this.batchBillList.length; j ++){
                this.totalPrice += Number(this.batchBillList[j].bill_sum_price);
            }
        }
        // 计算贴息
        async ratechange (index) {
            if(this.batchBillList[index].bill_rate < 0){
                this.$alert('请输入正确的利率！','提示',{
                    confirmButtonText: '确定',
                    customClass: 'custom-message-box',
                    callback: action => {
                        this.batchBillList[index].bill_rate = '';
                    }
                })
                return;
            }
            if(this.batchBillList[index].bill_rate >= 100){
                this.$alert('请输入正确的利率！','提示',{
                    confirmButtonText: '确定',
                    customClass: 'custom-message-box',
                    callback: action => {
                        this.batchBillList[index].bill_rate = '99';
                    }
                })
                return;
            }
            if(this.batchBillList[index].bill_every_plus < 0){
                this.$alert('请输入正确的每十万加！','提示',{
                    confirmButtonText: '确定',
                    customClass: 'custom-message-box',
                    callback: action => {
                        this.batchBillList[index].bill_every_plus = '';
                    }
                })
                return;
            }
            this.rateModel = {}
            //此处错误的利率和每十万也要计算利息。（上面已对错误值进行拦截）
            //原因：当用户手动清除输入框（自由竞价时可不填写），此时未通过校验不调接口，贴息金额未变，发布时与后台计算贴息对不上，导致无法发布
            // if(this.batchBillList[index].bill_rate > 0 && this.batchBillList[index].bill_rate < 100 || this.batchBillList[index].bill_every_plus > 0){
                let newDate = new Date();
                this.rateModel.start_time = moment(newDate).format('YYYY-MM-DD');
                this.rateModel.end_time = moment(this.batchBillList[index].bill_deadline_time).format('YYYY-MM-DD');
                this.rateModel.denomination = calc.div(this.batchBillList[index].bill_sum_price,10000);
                this.rateModel.interest_year = this.batchBillList[index].bill_rate;
                this.rateModel.every_plus = this.batchBillList[index].bill_every_plus;
                if(!this.batchBillList[index].bill_sum_price){
                    this.$alert('请输入票面金额！','提示',{
                        confirmButtonText: '确定',
                        customClass: 'custom-message-box',
                        callback: action => {}
                    })
                    return;
                }
                if(this.rateModel.end_time < this.rateModel.start_time){
                    this.$alert('票据到期日不能小于今天！','提示',{
                        confirmButtonText: '确定',
                        customClass: 'custom-message-box',
                        callback: action => {}
                    })
                    return;
                }
                if(!this.batchBillList[index].bill_deadline_time){
                    this.$alert('请选择到期日！','提示',{
                        confirmButtonText: '确定',
                        customClass: 'custom-message-box',
                        callback: action => {}
                    })
                    return;
                }
                let res = await this.$http.post('tools/calculator?func=mix',this.rateModel)
                if(res.status == 200){
                    this.batchBillList[index].bill_deal_rate = res.data.discount_interest;
                    this.batchBillList[index].bill_deal_price = res.data.discount_amount;
                    this.batchBillList[index].every_plus = res.data.every_plus_amount
                }
            // }
        }
        // 发布票据
        saveBill () {
            // 发布
            let listBankBillTrade = _.cloneDeep(this.batchBillList);
            for (let i = 0; i < listBankBillTrade.length; i ++) {
                if (!listBankBillTrade[i].account_id || listBankBillTrade[i].account_id == 0){
                    this.$alert(`请选择票据所在户！`,'提示',{
                        confirmButtonText: '确定',
                        customClass: 'custom-message-box'
                    })
                    return;
                }
                if (!listBankBillTrade[i].bill_front_photo_id){
                    this.$alert('您有票据正面图片未上传！','提示',{
                        confirmButtonText: '确定',
                        customClass: 'custom-message-box'
                    })
                    return;
                }
                if (!listBankBillTrade[i].bill_sum_price){
                    this.$alert('请输入票面金额！','提示',{
                        confirmButtonText: '确定',
                        customClass: 'custom-message-box'
                    })
                    return;
                }
                if (listBankBillTrade[i].bill_sum_price <= 0){
                    this.$alert('请输入正确的票面金额！','提示',{
                        confirmButtonText: '确定',
                        customClass: 'custom-message-box'
                    })
                    return;
                }
                if (!listBankBillTrade[i].bill_deadline_time){
                    this.$alert('请选择票据到期日！','提示',{
                        confirmButtonText: '确定',
                        customClass: 'custom-message-box'
                    })
                    return;
                }
                if (listBankBillTrade[i].bill_flaw_ids.length < 1){
                    this.$alert('请选择票据瑕疵！','提示',{
                        confirmButtonText: '确定',
                        customClass: 'custom-message-box'
                    })
                    return;
                }
                if (!listBankBillTrade[i].bidding_mode && this.checks == 1){
                    this.$alert('请选择交易方式！','提示',{
                        confirmButtonText: '确定',
                        customClass: 'custom-message-box'
                    })
                    return;
                }
            }
            let billForm = this.batchBillList;
            if(this.checks == 1){
                this.$http.post('seller/bill/addAndPushBills',billForm)
                    .then((response) => {
                        if(response.status == 200){
                            this.$alert('发布成功','提示',{
                                confirmButtonText: '确定',
                                customClass: 'custom-message-box',
                                callback: action => {
                                    this.$router.push('/seller/myBill');
                                }
                            })
                        }else if(response.status == 290){
                            //同时发布多个票，其中有的成功了，有的失败了，就删除成功的，保留失败的
                            this.uploadFailMap = response.data
                            this.$refs['uploadFail'].openDialog()
                        }
                    })
            }else if(this.checks == 2){
                // 录入
                this.$http.post('seller/bill/addBills',billForm)
                    .then((response) => {
                        if(response.status == 200){
                            this.$alert('录入成功','提示',{
                                confirmButtonText: '确定',
                                customClass: 'custom-message-box',
                                callback: action => {
                                    this.$router.push('/seller/myBill');
                                }
                            })
                        }else if(response.status == 290){
                            //同时发布多个票，其中有的成功了，有的失败了，就删除成功的(防止再次上传导致票据号重复)，保留失败的
                            this.uploadFailMap = response.data
                            this.$refs['uploadFail'].openDialog()
                        }
                    })
            }
        }
        //取消发布
        go_back () {
            history.back(-1)
        }
        //
        closeDialog () {
            this.$refs['uploadFail'].closeDialog()
            for (let i = this.uploadFailMap.successIndexs.length-1; i >= 0; i--){
                this.batchBillList.splice(this.uploadFailMap.successIndexs[i],1);
            }
            this.totalPrice = 0
            for (let j = 0; j< this.batchBillList.length; j ++) {
                this.totalPrice += Number(this.batchBillList[j].bill_sum_price);
            }
            this.billFaceNum = this.batchBillList.length
        }
        // 获取票据信息
        async getBillDetail () {
            let res = await this.$http.get(`bills/bill/${this.$route.params.id}`)
            if(res.status == 201){
                this.billModel = res.data;
                if (res.data.bill_back_files.length == 0){
                    this.form.bill_back_photo_address1 = 'https://hpx-pc.oss-cn-beijing.aliyuncs.com/billImgTem/bill-brank.png';
                    this.form.bill_back_photo_id1 = '';
                    this.form.bill_back_photo_address2 = 'https://hpx-pc.oss-cn-beijing.aliyuncs.com/billImgTem/bill-brank.png';
                    this.form.bill_back_photo_id2 = ''
                }else if(res.data.bill_back_files.length == 1){
                    this.form.bill_back_photo_address1 = res.data.bill_back_files[0].file_path;
                    this.form.bill_back_photo_id1 = res.data.bill_back_files[0].file_id;
                    this.form.bill_back_photo_address2 = 'https://hpx-pc.oss-cn-beijing.aliyuncs.com/billImgTem/bill-brank.png';
                    this.form.bill_back_photo_id2 = ''
                }else if(res.data.bill_back_files.length == 2){
                    this.form.bill_back_photo_address1 = res.data.bill_back_files[0].file_path;
                    this.form.bill_back_photo_id1 = res.data.bill_back_files[0].file_id;
                    this.form.bill_back_photo_address2 = res.data.bill_back_files[1].file_path;
                    this.form.bill_back_photo_id2 = res.data.bill_back_files[1].file_id;
                }
                this.batchBillList = [{
                    bill_front_photo_address: res.data.bill_front_photo_path,
                    bill_front_photo_id: res.data.bill_front_photo_id,
                    bill_back_photo_address1: this.form.bill_back_photo_address1,
                    bill_back_photo_id1: this.form.bill_back_photo_id1,
                    bill_back_photo_address2: this.form.bill_back_photo_address2,
                    bill_back_photo_id2: this.form.bill_back_photo_id2,
                    bill_type_id: 101,
                    bill_sum_price: res.data.bill_sum_price,
                    bill_flaw_ids: res.data.bill_flaw_ids || [2202],
                    bill_detail: true,
                    bidding_mode: res.data.bidding_mode,
                    trade_appoint_day: res.data.trade_appoint_day,
                    endorsement_number: res.data.endorsement_number,
                    flaw_append: res.data.flaw_append,
                    bill_number: res.data.bill_number,
                    acceptor_name: res.data.acceptor_name,
                    acceptor_cnaps: res.data.acceptor_cnaps,
                    bill_deadline_time: res.data.bill_deadline_time,
                    bill_back_files: res.data.bill_back_files,
                    bill_rate: res.data.bill_product_rate,
                    bill_every_plus: res.data.bill_product_every_plus,
                    bill_deal_rate: res.data.discount_interest,
                    bill_deal_price: res.data.discount_amount,
                    every_plus: res.data.every_plus,
                    account_id: res.data.account_information.account_id
                }]
                this.totalPrice = res.data.bill_sum_price;
                this.billFaceNum = 1;
            }
        }
        // 修改票据（重新录入，重新发布）
        async editBill () {
            let billPublishForm = {
                id: this.billModel.bill_id,
                billProductId: this.billModel.bill_product_id,
                acceptor_type_id: this.billModel.acceptor_type_id,
                ...this.batchBillList[0]
            }
            if (this.checks == 1){
                // 重新发布
                let res = await this.$http.put('seller/bill/updateAgainPushBill',billPublishForm)
                if(res.status < 300){
                    this.$alert('重新发布成功！','提示',{
                        confirmButtonText: '确定',
                        customClass: 'custom-message-box',
                        callback: action => {
                            this.$router.push('/seller/myBill');
                        }
                    })
                }
            } else if (this.checks == 2){
                // 重新录入
                let editForm = {
                    id: this.billModel.bill_id,
                    acceptor_type_id: this.billModel.acceptor_type_id,
                    ...this.batchBillList[0]
                }
                if(this.billModel.is_checked == -1){
                    let res = await this.$http.post('seller/bill/addBills',[editForm])
                    if(res.status < 300){
                        this.$alert('重新录入成功！','提示',{
                            confirmButtonText: '确定',
                            customClass: 'custom-message-box',
                            callback: action => {
                                this.$router.push('/seller/myBill');
                            }
                        })
                    }
                }else if(this.billModel.is_checked == 1){
                    let res = await this.$http.put('seller/bill/updateBillIsChecked',editForm)
                    if(res.status == 200){
                        this.$alert('重新录入成功！','提示',{
                            confirmButtonText: '确定',
                            customClass: 'custom-message-box',
                            callback: action => {
                                this.$router.push('/seller/myBill');
                            }
                        })
                    }
                }
            }
        }
        data () {
            return {
                pickerOptions: {
                    disabledDate(time) {
                        return time.getTime() < Date.now() - 8.64e7
                    }
                }
            }
        }
        @Watch('$route')
        ca(route){
            if(route.params.id == '0'){
                this.batchBillList = [{
                    bill_front_photo_address: 'https://hpx-pc.oss-cn-beijing.aliyuncs.com/billImgTem/bill-front.png',
                    bill_front_photo_id: '',
                    bill_back_photo_address1: 'https://hpx-pc.oss-cn-beijing.aliyuncs.com/billImgTem/bill-brank.png',
                    bill_back_photo_id1: '',
                    bill_back_photo_address2: 'https://hpx-pc.oss-cn-beijing.aliyuncs.com/billImgTem/bill-brank.png',
                    bill_back_photo_id2: '',
                    bill_type_id: 101,
                    bill_sum_price: '',
                    bill_flaw_ids: [1500],
                    bill_detail: true,
                    bidding_mode: 3210,
                    trade_appoint_day: 0,
                    endorsement_number: 0,
                    flaw_append: '',
                    bill_number: '',
                    acceptor_name: '',
                    acceptor_cnaps: '',
                    bill_deadline_time: '',
                    bill_back_files: [],
                    bill_rate: '',
                    bill_every_plus: '',
                    bill_deal_rate: '',
                    bill_deal_price: '',
                    every_plus: '',
                    account_id: ''
                }]
                this.totalPrice = '';
                this.billFaceNum = '';
            }
            this.checks = route.params.checks;
            this.billId = route.params.id;
            if(this.billId != '0'){
                this.getBillDetail();
            }
        }
        mounted () {
            this.getAccount();
            this.getPolicy();
            this.checks = this.$route.params.checks;
            this.billId = this.$route.params.id;
            if(this.billId != '0'){
                this.getBillDetail();
            }
        }
    }
</script>
<style lang="scss" scoped>
    .content{
        .detail_mian{
            margin-top: 0;
            margin-bottom: 20px;
        }
    }
    .posted-info {
        height: 50px;
        line-height: 50px;
    }
    .pack-up {
        width: 72px;
        height: 32px;
        border: 1px solid $__color-primary;
        background: $__color-bg-unimportance !important;
        color: $__color-primary;
        &:hover {
            border: 1px solid $__color-primary;
            background: $__color-bg-unimportance;
            color: $__color-primary;
        }
    }
    .icon-jiantou {
        font-size: 12px;
    }
    .icon-select:before{
        transform: rotate(70deg);
        -webkit-transform:rotate(70deg);
    }
    .title-price {
        margin-left: 40px;
        margin-right: 100px;
    }
    .title-price-i {
        color: $__color-remind;
        font-size: 20px;
    }
    .mian_block {
        border-top: none !important;
    }
    .posted-form-con {
        /*padding-left: 5%;*/
        padding-top: 12px;
        width: 90%;
        margin-left: 5%;
        margin-right: 5%;
    }
    .posted-rem {
        color: $__color-remind;
        & .el-button--text {
            color: $__color-link;
            text-decoration: underline;
        }
        height: 32px;
        font-size: 14px;
    }
    .posted-warm {
        background: $__color-bg-unimportance;
        line-height: 32px;
        padding-left: 4px;
        /*padding-left: 10px;*/
        /*margin-right: 80px;*/
    }
    .posted_title_img {
        .el-form-item {
            margin-bottom: 0;
        }
    }
    .posted-img-box {
        display: flex;
        justify-content: space-between;
        margin-left: 40px;
        margin-right: 40px;
    }
    .post-int-con {
        /*margin-left: 90px;*/
        width: 874px;
        margin: 0 auto;
        margin-top: 20px;
        .el-button {
            height: $height-int;
            padding: 6px 15px;
        }
    }
    .posted-int-width {
        /*width: 444px;*/
    }
    .post-int-mun {
        .el-input__inner {
            height: $height-int;
        }
    }
    .podted-show {
        background: $__color-bg-primary;
        height: 40px;
        margin-top: 30px;
        line-height: 40px;
        padding-left: 10%;
        padding-right: 8%;
        border-radius: 4px;
        font-size: 14px;
    }
    .bottom_btn {
        float: right;
    }
    .add-bill {
        width: 260px;
        padding: 8px 15px;
        height: $height-int;
        border: 1px solid $__color-link !important;
        color: $__color-link !important;
        background: $__color-bg-multiple !important;
        &:hover{
            border: 1px solid $__color-link;
            color: $__color-link;
            background: $__color-bg-multiple;
        }
    }
    .icon-tianjia {
        vertical-align: middle;
        margin-right: 2px;
    }
    .posted-submit-btn {
        display: flex;
        justify-content: center;
        margin-top: 20px;
        .el-button{
            font-size: 14px;
        }
    }
    .posted-flaw-i {
        height: 34px;
        line-height: 34px;
        border: 1px solid $__color-border;
        border-right: 0;
        border-top-left-radius: 4px;
        border-bottom-left-radius: 4px;
        display: inline-block;
        padding-left: 15px;
        color: $__color-label;
    }
    .img-box {
        width: 320px;
        height: 240px;
        border: 1px dashed $__color-border;
        box-sizing: border-box;
        display: flex;
        justify-content: center;
        align-items: center;
        position: relative;
        /*img {*/
            /*max-width: 228px;*/
            /*max-height: 154px;*/
        /*}*/
    }
    .delete-bill-btn {
        position: absolute;
        top: 0;
        right: 0;
        cursor: pointer;
    }
    .icon-shangchuanshanchu {
        color: $__color-remind;
        font-size: 18px;
    }
    .post-delete {
        width: 78px;
        height: 32px;
        background: $__color-bg-delete !important;
        color: $__color-remind !important;
        border: 1px solid $__color-remind !important;
    }
    .posted-count-box {
        margin-left: 80px;
    }
    .top_btn {
        .el-button--small {
            font-size: $__little-font-size;
            .iconfont {
                font-size: $__little-font-size;
            }
        }
    }
    .iconfont_right {
        margin-right: 4px;
    }
    .title-flaw {
        margin-left: 80px;
    }
    .uploadFailTips{
        line-height: 24px;
        font-size: 14px;
        color: $__color-label;
        .uploadFailTips_inner{
            font-weight: 600;
        }
    }
</style>
<style lang="scss">
    .post-int {
        .el-input__inner {
            height: $height-int-register;
            line-height: $height-int-register;
            border-left: none;
            border-bottom-left-radius: 0;
            border-top-left-radius: 0;
            border-color: $__color-border !important;
        }
        .el-input-number {
            width: 90px;
            line-height: $height-int-register;

        }
        .el-input-number.is-controls-right .el-input__inner {
            padding-left: 8px;
        }
    }
    .post-int-mun {
        .el-input__inner {
            height: $height-int-register;
            line-height: $height-int-register;
        }
    }
    .approve-int {
        .el-input__inner {
            width: $width-int-register !important;
            height: $height-int-register !important;
            line-height: $height-int-register !important;
        }
    }
    .form-box {
        .el-form-item__label {
            line-height: $height-int-register;
        }
        .el-form-item__content{
            // line-height: $height-int-register;
            line-height: 28px;
            color: $__color-remind;
            font-size: 20px;
        }
        .el-input__icon{
            line-height: 32px;
        }
    }
    .bill-flaw {
        .el-checkbox-button{
            .el-checkbox-button__inner{
                font-size: 14px;
                padding-top: 8px;
                padding-bottom: 8px;
            }
        }
        .turn-flaw {
            .el-input__inner {
                border: none;
            }
            .el-input {
                width: 60px;
            }
            .el-checkbox-button__inner {
                padding: 0 8px;
                // height: $height-int-register;
                height: 32px;
            }
            .el-input__inner {
                height: 30px;
                line-height: 32px;
                width: 60px;
                padding: 0 2px;
                text-align: center;
                // margin-top: 2px;
            }
            .el-input-number {
                line-height: 36px;
            }
            .el-input-number.is-controls-right .el-input-number__decrease {
                bottom: 4px;
            }
            .el-input-number.is-controls-right .el-input-number__increase, .el-input-number.is-controls-right .el-input-number__decrease {
                line-height: 17px;
            }
            .el-input-number__increase, .el-input-number__decrease {
                top: 4px;
            }
            .el-input-number {
                width: 100px;
            }
        }
        .is-checked {
            .el-input__inner {
                background: $__color-primary;
                color: #fff;
            }
        }
    }
    .flaw-num {
        .el-input-number{
            width: 150px;
        }
        .el-input-number.is-controls-right .el-input-number__increase, .el-input-number.is-controls-right .el-input-number__decrease {
            line-height: 15px;
        }
        .el-input-number.is-controls-right .el-input-number__decrease {
            bottom: 4px;
        }
        .el-input-number__increase, .el-input-number__decrease {
            top: 4px;
        }
        .el-input__inner {
            height: $height-int;
            line-height: $height-int;
        }
    }
    .posted-count {
        height: 40px;
        line-height: 40px;
        font-size: 14px;
        margin-bottom: 22px;
        span:nth-child(1){
            color: $__color-show;
        }
        span:nth-child(2){
            color: $__color-remind;
            font-size: 20px;
        }
    }
    .posted_title_img {
        .el-form-item__label {
            width: 125px !important;
        }
        /*.el-form-item__content {*/
            /*width: 125px !important;*/
        /*}*/
    }
    .posted-int-width {
        .el-input__suffix {
            right: 13% !important;
        }
    }
</style>